﻿#region header

//  ***************************************************************************
// *                                LICENSE
//  ***************************************************************************
//  * "Haplous" .NET Simple 2D Game Framework
//  * 
//  * Copyright © Jonathan Evans 2014-2014
//  * 
//  * This program is free software; you can redistribute it and/or
//  * modify it under the terms of the GNU General Public License
//  * as published by the Free Software Foundation; either version 2
//  * of the License, or (at your option) any later version.
//  * 
//  * This program is distributed in the hope that it will be useful,
//  * but WITHOUT ANY WARRANTY; without even the implied warranty of
//  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  * GNU General Public License for more details.
//  * 
//  * You should have received a copy of the GNU General Public License
//  * along with this program; if not, write to the Free Software
//  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//  * 
//  ***************************************************************************
//  * 
//  * Contact: opencollar@googlemail.com
//  * 
//  * Website: http://haplous.codeplex.com/
//  *
//  * NuGet:   http://www.nuget.org/packages/haplous/
//  *
//  * File:    Haplous/Haplous/Dispatcher.cs
//  *
//  ***************************************************************************

#endregion

using System;
using System.Windows;
using System.Windows.Threading;

using Haplous.Correctness;

using JetBrains.Annotations;

namespace Haplous.Core
{
    /// <summary>
    ///     The ways in which the dispatcher may be validated.
    /// </summary>
    internal enum DispatcherValidationKind
    {
        /// <summary>
        ///     The current thread must be the dispatcher thread.
        /// </summary>
        MustBeOnDispatcher,

        /// <summary>
        ///     /// The current thread must NOT be the dispatcher thread.
        /// </summary>
        MustNotBeOnDispatcher
    }

    /// <summary>
    ///     Support methods to simplify interactions with the dispatcher.
    /// </summary>
    internal static class Dispatcher
    {
        public static void ValidateDispatcher(DispatcherValidationKind validation)
        {
            validation.ValidateEnumArgument("validation");

            var application = Application.Current;
            if(application == null)
            {
                if(validation == DispatcherValidationKind.MustNotBeOnDispatcher)
                    return;

                throw new InvalidOperationException("Unable to invoke on dispatcher if no application has been defined.");
            }

            var dispatcher = application.Dispatcher;
            if(dispatcher == null)
            {
                if(validation == DispatcherValidationKind.MustNotBeOnDispatcher)
                    return;
                throw new InvalidOperationException("Unable to invoke on dispatcher if the application has no dispatcher defined.");
            }

            if(dispatcher.CheckAccess())
            {
                if(validation == DispatcherValidationKind.MustNotBeOnDispatcher)
                    throw new InvalidOperationException("Unable to perform operation whilst on dispatcher thread.");
            }
            else
            {
                if(validation == DispatcherValidationKind.MustBeOnDispatcher)
                    throw new InvalidOperationException("Unable to perform operation unless on dispatcher thread.");
            }
        }

        /// <summary>
        ///     Invokes the specified action on the dispatcher thread.
        /// </summary>
        /// <param name="action">The action to invoke.</param>
        public static void Invoke([NotNull] Action action)
        {
            action.ValidateArgument("action");

            var application = Application.Current;
            if(application == null)
                throw new InvalidOperationException("Unable to invoke on dispatcher if no application has been defined.");

            var dispatcher = application.Dispatcher;
            if(dispatcher == null)
                throw new InvalidOperationException("Unable to invoke on dispatcher if the application has no dispatcher defined.");

            if(dispatcher.CheckAccess())
                action.Invoke();
            else
                dispatcher.Invoke(DispatcherPriority.Normal, action);
        }
    }
}